import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../mov_entity.dart';

class mainpage extends StatefulWidget {
  const mainpage({Key? key}) : super(key: key);

  @override
  _MainpageState createState() => _MainpageState();
}

class _MainpageState extends State<mainpage> {
  List<Widget> _list = [];

  @override
  void initState() {
    super.initState();
    loaddata();
  }
  loaddata() async {
    String url =
        "https://api.wmdb.tv/api/v1/top?type=Imdb&skip=0&limit=20&lang=Cn";
    var dio = await Dio();
    dio
        .get(url, options: Options(responseType: ResponseType.plain))
        .then((value) {
      print(value);

      List<dynamic> list = jsonDecode(value.toString());
      List<MovEntity> mov = list.map((e) {
        return MovEntity().fromJson(e);
      }).toList();
      for (int i = 0; i < mov.length; i++) {
        setState(() {
          _list.add(getcar(
              mov[i].data![0].poster.toString(),
              mov[i].data![0].name.toString(),
              mov[i].data![0].genre.toString(),
              mov[i].data![0].country.toString(),
              mov[i].data![0].description.toString()));
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: this._list.length > 0
              ? ListView(
            children: this._list,
          )
              : Text("data")),
        debugShowCheckedModeBanner: false
    );
  }
}

Card getcar(
    String url,
    String name,
    String geren,
    String country,
    String description,
    ) {
  return Card(
    //  设置圆角
    shape: RoundedRectangleBorder(
      //形状修改圆角
      borderRadius: BorderRadius.all(Radius.circular(9)),
    ),
    color: Colors.white,
    elevation: 4.0,
    shadowColor: Colors.grey,
    child: getitem(
      url,
      name,
      geren,
      country,
      description,
    ),
  );
}

Widget getitem(
    String url,
    String name,
    String geren,
    String country,
    String description,
    ) {
  return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      SizedBox(
        height: 4,
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 3,
          ),
          Image(
            image: NetworkImage(url),
            width: 100,
            height: 150,
            fit: BoxFit.cover,
          ),
          SizedBox(
            width: 10,
          ),
          Expanded(
            flex: 1,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  name,
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text("2022", style: TextStyle(color: Colors.black54)),
                    SizedBox(
                      width: 2,
                    ),
                    Text(geren, style: TextStyle(color: Colors.black54),overflow: TextOverflow.clip,),
                    SizedBox(
                      width: 2,
                    ),
                    Expanded(flex: 1,
                        child: Text(country, style: TextStyle(color: Colors.black54),softWrap: true,maxLines: 1,))
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Text(
                  description,
                  maxLines: 3,
                  softWrap: true,
                  textAlign: TextAlign.start,
                  style: TextStyle(color: Colors.black54),
                )
              ],
            ),
          )
        ],
      ),
      SizedBox(
        height: 3,
      )
    ],
  );
}
